package com.sskj.lib.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.R;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.CertificationBean;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.lib.widget.TipDialog;
import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Lee on 2018/2/28 0028.
 */

public abstract class BaseFragment<P extends IPresenter> extends RxFragment implements IBaseView {

    protected P mPresenter;
    private Unbinder mUnbinder;
    private View inflate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        RudenessScreenHelper.resetDensity(getContext(), 750);

        if (inflate == null) {
            inflate = inflater.inflate(getLayoutId(), null);
            mPresenter = getPresenter();
            mPresenter.attachView(this, getActivity());
            mUnbinder = ButterKnife.bind(this, inflate);
            initView();
            initData();
        }
        if (inflate.getParent() != null) {
            ((ViewGroup) inflate.getParent()).removeView(inflate);
        }
        return inflate;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter.cancelRequest();
        }
        mUnbinder.unbind();
        RxBus.getDefault().unregister(this);

    }

    public boolean isEmpty(TextView textView) {
        return textView == null || TextUtils.isEmpty(textView.getText());
    }

    public boolean isEmpty(EditText editText) {
        return editText == null || TextUtils.isEmpty(editText.getText().toString());
    }

    public boolean isEmpty(String str) {
        return str == null || TextUtils.isEmpty(str);
    }

    public void toastHint(EditText editText) {
        ToastUtils.show(editText.getHint());
    }

    public String getText(TextView textView) {
        if (textView != null) {
            return textView.getText().toString();
        }
        return "";
    }


    protected abstract int getLayoutId();

    protected abstract P getPresenter();

    public void initView() {

    }

    public void initData() {
    }

    /*1=>’未认证’, 2=>’请耐心等待审核’, 3=>’已通过’,4=>’审核拒绝*/
    public boolean checkCetification() {
        if (!SPUtil.get(SPConfig.LOGIN, false)) {
            ARouter.getInstance().build(RConfig.APP_LOGIN_NUMBER).navigation();
            getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
//            new TipDialog(getActivity())
//                    .setTitleText(App.INSTANCE.getString(R.string.lib_tip))
//                    .setContentText(App.INSTANCE.getString(R.string.libExceptionUtil7))
//                    .setListener(dialog -> ARouter.getInstance().build(RConfig.APP_LOGIN_NUMBER).navigation())
//                    .show();
            return false;
        }
        if (SPUtil.get(SPConfig.CETIFICATION, 0) != 3) {
            switch (SPUtil.get(SPConfig.CETIFICATION, -1)) {
                case 1:
                    new TipDialog(getActivity())
                            .setTitleText(App.INSTANCE.getString(R.string.lib_tip))
                            .setContentText(App.INSTANCE.getString(R.string.lib_bottomDialog1))
                            .setCancelText(App.INSTANCE.getString(R.string.lib_cancel))
                            .setConfirmText(App.INSTANCE.getString(R.string.lib_baseActivity5))
                            .setListener(dialog -> ARouter.getInstance().build(RConfig.APP_ACTIVITY_CERTIFICATION).navigation())
                            .show();
//                    BottomDialog.Authentication(getActivity(), 1);
                    break;
                case 2:
                    ToastUtils.show(App.INSTANCE.getString(R.string.lib_baseActivity6));
                    break;
                case 4:
                    new TipDialog(getActivity())
                            .setTitleText(App.INSTANCE.getString(R.string.lib_tip))
                            .setContentText(App.INSTANCE.getString(R.string.lib_bottomDialog2))
                            .setCancelText(App.INSTANCE.getString(R.string.lib_cancel))
                            .setConfirmText(App.INSTANCE.getString(R.string.lib_baseActivity5))
                            .setListener(dialog -> ARouter.getInstance().build(RConfig.APP_ACTIVITY_CERTIFICATION).navigation())
                            .show();
//                    BottomDialog.Authentication(getActivity(), 4);
                    break;
            }
            return false;
        }
        return true;
    }

}
